package cn.itcast.user.Userdao;

import cn.itcast.user.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;


@Component
public class UserDao {
    @Autowired
    private RestTemplate restTemplate;
    private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息
     @HystrixCommand(fallbackMethod = "queryUserByIdFallback")
    public User queryUserById(Long id) {
      Long begin=System.currentTimeMillis();
      String url="http://user-service/user/"+id;
      User user=this.restTemplate.getForObject(url,User.class);
      Long end=System.currentTimeMillis();
        logger.info("访问用时:{}",end-begin);

return user;
    }
    public User queryUserByIdFallback(Long id){
         User user=new User();
         user.setId(id);
         user.setName("用户信息查询异常");
         return user;
    }
}
